package com.xqboss.web.controller.synthesis;

import com.xqboss.apps.dto.PageDto;
import com.xqboss.apps.dto.synthesis.*;
import com.xqboss.apps.service.synthesis.SynthesisPlanService;
import com.xqboss.apps.vo.synthesis.SynthesisPlanPageVo;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 合成方案
 * </p>
 *
 * @author 木鱼
 * @since 2023-11-20
 */
@RestController
@Slf4j
@Api(tags = "合成方案")
@RequestMapping("/web/synthesisPlan")
@RequiredArgsConstructor
public class WebSynthesisPlanController extends BaseController {
    private final SynthesisPlanService basicService;

    /**
     * 合成方案 列表查询
     *
     * @param search 方案名称
     */
    @PreAuthorize("@ss.hasPermi('web:synthesisPlan:list')")
    @GetMapping("/list")
    @ApiOperation("列表")
    public R<List<SynthesisPlanPageVo>> loadList(Integer pageNum, Integer pageSize, String search) {
        PageDto pageDto = new PageDto();
        pageDto.setPageNum(pageNum);
        pageDto.setPageSize(pageSize);
        return R.ok(basicService.selectBasicList(pageDto, search));
    }

    /**
     * 合成方案 根据id查询
     */
    @PreAuthorize("@ss.hasPermi('web:synthesisPlan:selectById')")
    @GetMapping("/selectById")
    @ApiOperation("详情")
    public R<SynthesisPlanDetailDto> selectById(@RequestParam("id")
                                                @ApiParam(value = "方案规则ID", required = true)
                                                @NotNull(message = "请输入方案规则ID") Long id) {
        return R.ok(basicService.selectByIdBasic(id));
    }

    /**
     * 合成方案 修改
     */
    @PreAuthorize("@ss.hasPermi('web:synthesisPlan:update-count')")
    @RepeatSubmit(interval = 1000, justUrl = true)
    @PostMapping("/update-count")
    @ApiOperation("修改数量")
    public R<Boolean> updatePlan(@Validated @RequestBody SynthesisPlanUpdateDto record) {
        return R.ok(basicService.updatePlan(record));
    }

    @PreAuthorize("@ss.hasPermi('web:synthesisPlan:edit')")
    @RepeatSubmit(interval = 1000, justUrl = true)
    @PostMapping("/edit")
    @ApiOperation("修改-规则")
    public R<Boolean> update(@Validated @RequestBody WebSynthesisConsumeEditDto params) {
        return R.ok(basicService.edit(params));
    }

    /**
     * 合成方案产出库存 修改
     */
//    @PostMapping("/updateStock")
//    @ApiOperation("修改-产出库存")
//    public R<Boolean> updateStock(@Validated @RequestBody SynthesisPlanStockDto record) throws Exception {
//        return R.ok(basicService.updateStock(record));
//    }

    /**
     * 合成方案 新增
     */
    @PreAuthorize("@ss.hasPermi('web:synthesisPlan:insert')")
    @RepeatSubmit(interval = 1000, justUrl = true)
    @PostMapping("/insert")
    @ApiOperation("新增")
    public R<Boolean> insert(@Validated @RequestBody SynthesisPlanDetailDto record) throws Exception {
        return R.ok(basicService.saveBasic(record));
    }

    /**
     * 合成方案
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('web:synthesisPlan:delete')")
    @PostMapping("/delete")
    @ApiOperation("删除")
    public R<Boolean> delete(@Validated @RequestBody WebSynthesisPlanDelDto params) throws Exception {
        return R.ok(basicService.removeBasic(params));
    }
}
